from django.db import models

from user.models import UserModel


# Create your models here.


class CateModel(models.Model):
    c_name = models.CharField(max_length=50, verbose_name='分类名称')
    parent = models.ForeignKey('self', null=True, blank=True, on_delete=models.CASCADE, verbose_name='父级分类',
                               related_name='children')
    is_home = models.BooleanField(default=False, verbose_name='是否展示在首页')

    class Meta:
        db_table = 'sxcate'

    def __str__(self):
        return self.c_name


class OriginModel(models.Model):
    o_name = models.CharField(max_length=50, verbose_name='产地')

    class Meta:
        db_table = 'sxorigin'

    def __str__(self):
        return self.o_name


class GoodsModel(models.Model):
    g_img = models.ImageField(upload_to="upload/", null=True, blank=True, verbose_name="商品图片")
    g_name = models.CharField(max_length=50, verbose_name='商品名称')
    cate = models.ForeignKey(CateModel, on_delete=models.CASCADE, verbose_name='商品分类')
    origin = models.ForeignKey(OriginModel, on_delete=models.CASCADE, verbose_name='商品产地')
    storage = models.IntegerField(
        choices=(('1', '冷藏'), ('2', '常温'), ('3', '冰冻'), ('4', '控温')),
        default=1, verbose_name='存储方式'
    )
    g_price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='商品价格')
    sales = models.IntegerField(default=0, verbose_name='销量')
    is_have = models.BooleanField(default=True, verbose_name='是否有货')
    is_home = models.BooleanField(default=False, verbose_name='是否展示在首页')
    g_desc = models.CharField(max_length=200, verbose_name='商品描述', default="暂无")

    class Meta:
        db_table = 'sxgoods'

    def __str__(self):
        return self.g_name


class AssessModel(models.Model):
    user = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name='用户')
    goods = models.ForeignKey(GoodsModel, on_delete=models.CASCADE, verbose_name='商品')
    comment = models.CharField(max_length=200, verbose_name='评论')
    a_time = models.DateField(verbose_name='评论时间', default='2025-01-01')

    class Meta:
        db_table = 'sxassess'
